package display
{
	import events.ControllerEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import controller.Controller;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import org.osmf.events.MediaPlayerStateChangeEvent;
	import events.ViewEvent;
	
	import qbrick.professional.v4.modules.mediaplayer.assets.Controls;
	
	/**
	 * ...
	 * @author Henrik Riise Hansen
	 */
	
	public class DisplayControl extends Sprite
	{
		private var shell:Shell;
		private var control:Controller;
		private var controlPanel:Controls;
		private var scrupper:MovieClip;
		private var progressBar:MovieClip;
		private var dragging:Boolean;
		
		public function DisplayControl(_shell:Shell, _controller:Controller)
		{
			shell = _shell;
			
			control = _controller;
			control.addEventListener(ControllerEvent.STATE, onStateChange);
			control.addEventListener(ControllerEvent.PROGRESS, onProgress);
			
			controlPanel = new Controls;
			controlPanel.y = 409;
			
			scrupper = controlPanel.timeline.scrubber;
			progressBar = controlPanel.timeline.progressBar;
			
			dragging = false;
			
			build();
		}
		
		private function build():void
		{
			// scrupper
			
			scrupper.buttonMode = true;
			scrupper.enabled = false;
			scrupper.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
			scrupper.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
			
			// pausebutton
			
			controlPanel.pauseButton.visible = false;
			controlPanel.pauseButton.buttonMode = true;
			controlPanel.pauseButton.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			controlPanel.pauseButton.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			controlPanel.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
			controlPanel.pauseButton.addEventListener(MouseEvent.CLICK, onClick);
			
			// playbutton
			
			controlPanel.playButton.buttonMode = true;
			controlPanel.playButton.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			controlPanel.playButton.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			controlPanel.playButton.addEventListener(MouseEvent.CLICK, onClick);
			
			addChild(controlPanel);
		}
		
		private function onStopDragOutside(e:MouseEvent):void 
		{
			dragging = false;
			
			scrupper.stopDrag();
		}
	
		private function onProgress(e:ControllerEvent):void 
		{
			if (!dragging)
			{
				scrupper.x = (e.data.time / e.data.duration * 409) ;
				
				progressBar.width = scrupper.x ;
			}
		}
		
		private function onScrupperMove(e:MouseEvent):void 
		{
			progressBar.width = scrupper.x ;
		}
		
		
		private function onStopDrag(e:MouseEvent):void 
		{
			dispatchEvent(new ViewEvent(ViewEvent.SEEK, { seek:scrupper.x } ));
			
			dragging = false;
			
			scrupper.stopDrag();
		}
		
		private function onStartDrag(e:MouseEvent):void 
		{
			dragging = true;
			
			scrupper.startDrag(false, new Rectangle(0, 17, 409, 0));
		}
		
		private function onMouseOut(e:MouseEvent):void
		{
			e.currentTarget.gotoAndStop('UP');
		}
		
		private function onMouseOver(e:MouseEvent):void
		{
			e.currentTarget.gotoAndStop('OVER');
		}
		
		private function onClick(e:MouseEvent):void
		{
			e.currentTarget.gotoAndStop('DOWN');
			
			switch(e.currentTarget.name)
			{
				case 'playButton':
				dispatchEvent(new ViewEvent(ViewEvent.PLAY));
				break;
				
				case 'pauseButton':
				dispatchEvent(new ViewEvent(ViewEvent.PAUSE));
				break;
			}
		
		}
		
		private function disable():void
		{
			controlPanel.pauseButton.gotoAndStop('DISABLED');
			controlPanel.playButton.gotoAndStop('DISABLED');
		}
		
		private function enable():void
		{
			controlPanel.pauseButton.gotoAndStop('UP');
			controlPanel.playButton.gotoAndStop('UP');
		}
		
		private function onStateChange(e:ControllerEvent):void
		{
			trace('state: ' + e.data.state);
			
			switch (e.data.state)
			{
				case 'loading':
				disable();
				break;
				
				case 'buffering':
				disable();
				break;
				
				case 'paused':
				enable();
	
				controlPanel.pauseButton.visible = false;
				controlPanel.playButton.visible = true;
				break;
				
				case 'playing':
				enable();
				
				controlPanel.playButton.visible = false;
				controlPanel.pauseButton.visible = true;
				break;
				
				case 'ready':
				enable();
				
				controlPanel.pauseButton.visible = false;
				controlPanel.playButton.visible = true;
				break;
				
				case 'playbackerror':
				disable();
				break;
			}
		}
	}
}